var handler_login_success = function(){
    // 跳转至管理主界面
    window.location = "manage.html";
};
// Functions that fire (success or failure) when the server responds. 
// The one that executes is determined by the 
// response that comes from login.asp as seen below. The server would 
// actually respond with valid JSON, 
// something like: response.write "{ success: true}" or 
// response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
// depending on the logic contained within your server script.
// If a success occurs, the user is notified with an alert messagebox, 
// and when they click "OK", they are redirected to whatever page
// you define as redirect.
var handler_login_failure = function( form, action ){
    if( action.failureType == "server" ){
        var obj = Ext.util.JSON.decode( action.response.responseText );
        Ext.Msg.alert( "验证失败", obj.errors.reason );
    }else{
        var msg = "无法连接服务器!原因为：";
        msg += action.response.responseText;
        Ext.Msg.alert( "错误", msg );
    }
};
var handler_login = function(){
    formLogin.getForm().submit({
        method: "POST",
        waitTitle: "正在连接服务器......",
        waitMsg: "正在传送验证数据......",
        success: handler_login_success,
        failure: handler_login_failure
    });
};
var inputUsername = {
    fieldLabel: "用户名",
    name: "loginUsername",
    allowBlank: false
};
var inputPassword = {
    fieldLabel: "密码",
    name: "loginPassword",
    allowBlank: false
};
var buttonLogin = {
    text: "登录",
    formBind: true,
    handler: handler_login
};
var formLogin = new Ext.FormPanel({
    labelWidth: 70,
    url: "/dyn/login/",
    frame: true,
    title: "用户登录",
    defaultType: "textfield",
    monitorValid: true,
    items: [ inputUsername, inputPassword ],
    buttons: [buttonLogin, ]
});
var loginWin = new Ext.Window({
    layout: "fit",
    width: 260,
    height: 150,
    closable: false,
    resizable: false,
    frame: true,
    border: false,
    items: [ formLogin, ]
});
Ext.onReady(function(){
    Ext.QuickTips.init();
    loginWin.show();
});
